<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "category".
 *
 * @property integer $id
 * @property integer $group_id
 * @property string $name
 * @property integer $sort
 * @property string $keywords
 * @property string $description
 * @property string $thumb
 * @property integer $create_at
 * @property integer $create_by
 * @property integer $status
 * @property string $logo
 * @property integer $dynum
 *
 * @property User $createBy
 * @property CategoryGroup $group
 * @property UserCategory[] $userCategories
 * @property User[] $users
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['group_id', 'sort', 'create_at', 'create_by', 'status', 'dynum'], 'integer'],
            [['name'], 'required'],
            [['name'], 'string', 'max' => 20],
            [['keywords'], 'string', 'max' => 100],
            [['description', 'thumb', 'logo'], 'string', 'max' => 255],
            [['create_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['create_by' => 'id']],
            [['group_id'], 'exist', 'skipOnError' => true, 'targetClass' => CategoryGroup::className(), 'targetAttribute' => ['group_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'group_id' => 'Group ID',
            'name' => 'Name',
            'sort' => 'Sort',
            'keywords' => 'Keywords',
            'description' => 'Description',
            'thumb' => 'Thumb',
            'create_at' => 'Create At',
            'create_by' => 'Create By',
            'status' => 'Status',
            'logo' => 'Logo',
            'dynum' => 'Dynum',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCreateBy()
    {
        return $this->hasOne(User::className(), ['id' => 'create_by']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGroup()
    {
        return $this->hasOne(CategoryGroup::className(), ['id' => 'group_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserCategories()
    {
        return $this->hasMany(UserCategory::className(), ['cate_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id' => 'user_id'])->viaTable('user_category', ['cate_id' => 'id']);
    }
}
